package com.hardware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hardware.common.CustomException;
import com.hardware.dto.ShopDto;
import com.hardware.entity.Shop;
import com.hardware.entity.ShopType;
import com.hardware.mapper.ShopMapper;
import com.hardware.service.ShopService;
import com.hardware.service.ShopTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Autowired
    private ShopTypeService shopTypeService;

    @Override
    public void saveWithType(ShopDto shopDto) {
        this.save(shopDto);

//        Long shopId = shopDto.getId();
//
//        List<ShopType> types = shopDto.getTypes();
//        types = types.stream().map((item) -> {
//            item.setShopId(shopId);
//            return item;
//        }).collect(Collectors.toList());
//
//        shopTypeService.saveBatch(types);
    }

    public ShopDto getByIdWithType(Long id) {
        //查询商品基本信息，从shop表查询
        Shop shop = this.getById(id);

        ShopDto shopDto = new ShopDto();
        BeanUtils.copyProperties(shop,shopDto);

        /*//查询当前商品对应的口味信息，从shop_type表查询
        LambdaQueryWrapper<ShopType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopType::getShopId,shop.getId());
        List<ShopType> types = shopTypeService.list(queryWrapper);
        shopDto.setTypes(types);*/

        return shopDto;
    }

    public void updateWithType(ShopDto shopDto) {
        //更新shop表基本信息
        this.updateById(shopDto);

       /* //清理当前商品对应类型---shop_type表的delete操作
        LambdaQueryWrapper<ShopType> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShopType::getShopId,shopDto.getId());

        shopTypeService.remove(queryWrapper);

        //添加当前提交过来的口味数据---shop_type表的insert操作
        List<ShopType> types = shopDto.getTypes();

        types = types.stream().map((item) -> {
            item.setShopId(shopDto.getId());
            return item;
        }).collect(Collectors.toList());

        shopTypeService.saveBatch(types);*/
    }

    /**
     * 删除商品，同时需要删除商品和类型的关联数据
     * @param ids
     */
    @Transactional
    public void removeWithShop(List<Long> ids) {
        //select count(*) from shop where id in (1,2,3) and status = 1
        //查询商品状态，确定是否可用删除
        LambdaQueryWrapper<Shop> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Shop::getId,ids);
        queryWrapper.eq(Shop::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("商品正在售卖中，不能删除");
        }

        //如果可以删除，先删除商品表中的数据---shop
        this.removeByIds(ids);

        //delete from shop_dish where shop_id in (1,2,3)
        LambdaQueryWrapper<ShopType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ShopType::getShopId,ids);
        //删除关系表中的数据----shop_dish
        shopTypeService.remove(lambdaQueryWrapper);
    }

    /**
     * 添加商品的库存
     * @param shopDto
     */
    @Override
    public void addStock(ShopDto shopDto) {
        Shop shop = getById(shopDto.getId());
        shop.setStock(shop.getStock() + shopDto.getStock());
        updateById(shop);
    }


}
